package com.online.olsrootserver.controller;

import com.online.olsrootserver.entity.Agent;
import com.online.olsrootserver.service.AgentService;
import com.online.olsrootserver.service.FindAentServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by littleheng on 2018/4/26
 */
@RestController
@RequestMapping("/agent")
public class AgentController {

    @Autowired
    private AgentService agentService;

    @Autowired
    private FindAentServer sessionManageClusterServer;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @CrossOrigin
    public Map login (@RequestParam("account") String account, @RequestParam("password")String password) {

        Map<String, String> maps = new HashMap<>();
        Agent agent = this.agentService.getAgentByPassword(account, password);
        if (agent != null && !"".equals(agent.getId())) {
            String uuidStr = UUID.randomUUID().toString().replace("-", "");

            Map<String, Object> params = new HashMap<>();
            params.put("service_id", Integer.parseInt(agent.getId()));
            params.put("token", uuidStr);

            Map result = this.sessionManageClusterServer.saveToken(params);
            System.out.println(result);
            if (Integer.parseInt(result.get("result").toString()) == 0) {
                maps.replace("result", "0");
                maps.put("token", uuidStr);
                maps.put("agentId", agent.getId());
                maps.put("companyId", agent.getCompanyId());
            }
        }
        return maps;
    }
}